<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['alarm:display:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['alarm:display:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:display:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['alarm:display:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="displayList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="事件Id" align="center" prop="eventId" /> -->
      <el-table-column label="显示条件" align="center" prop="conditions" />
      <el-table-column label="显示单位" align="center" prop="unit" />
      <el-table-column label="计算公式" align="center" prop="formula" />
      <el-table-column label="保留小数位数" align="center" prop="pointLength" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.monitor_display_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['alarm:display:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['alarm:display:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监测结果显示对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="优先级" prop="priority">
          <el-input-number v-model="form.priority" :min="1" :max="10" label="请输入优先级"></el-input-number>
        </el-form-item>
        <el-form-item label="监听场地" prop="siteId">
          <el-select v-model="form.siteId" placeholder="请选择监听场地">
            <el-option v-for="item in sites" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监听变量" prop="priority">
          <el-select v-model="form.deviceDefinedId" placeholder="请选择设备" @change="getDeviceEvents">
            <el-option v-for="item in deviceDefines" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
          <el-select v-model="form.eventId" placeholder="请选择变量" style="margin-left:30px;">
            <el-option v-for="item in deviceEvents" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示条件" prop="conditions">
          <el-input v-model="form.conditions" placeholder="请输入显示条件" />
        </el-form-item>
        <el-form-item label="显示单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入显示单位" />
        </el-form-item>
        <el-form-item label="计算公式" prop="formula">
          <el-input v-model="form.formula" placeholder="请输入计算公式" />
        </el-form-item>
        <el-form-item label="保留小数点后" prop="pointLength">
          <el-input-number v-model="form.pointLength" :min="1" :max="10" label="位"></el-input-number>&nbsp;&nbsp;位
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.monitor_display_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.common_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDisplay, getDisplay, delDisplay, addDisplay, updateDisplay } from "@/api/alarm/display";
import { optionSites } from "@/api/site/site";
import { optionDefineds } from "@/api/ztIot/defined";
import { optionEvents } from "@/api/alarm/deviceEvent";
export default {
  name: "Display",
  dicts: ['monitor_display_type', 'common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监测结果显示表格数据
      displayList: [],
      deviceDefines:[],
      deviceEvents:[],
      sites:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eventId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eventId: [
          { required: true, message: "事件Id不能为空", trigger: "blur" }
        ],
        conditions: [
          { required: true, message: "显示条件不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "显示单位不能为空", trigger: "blur" }
        ],
        formula: [
          { required: true, message: "计算公式不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        state: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeviceDefines();
    this.getSites();
  },
  methods: {
    /** 查询监测结果显示列表 */
    getList() {
      this.loading = true;
      listDisplay(this.queryParams).then(response => {
        this.displayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDeviceDefines(){
      optionDefineds().then(res => {
        this.deviceDefines = res.data
      })
    },
    //查询设备变量
    getDeviceEvents(_definedId){
      this.deviceEvents = []
      optionEvents({definedId:_definedId,type:'01'}).then( res => this.deviceEvents = res.data)
    },
    getSites(){
      optionSites().then( res => this.sites = res.data)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        priority:1,
        deviceDefinedId:null,
        siteId:null,
        eventId: null,
        conditions: null,
        unit: null,
        formula: null,
        type: null,
        state: 1,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        eventId:this.eventId,
        pointLength:2
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监测结果显示";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDisplay(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改监测结果显示";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDisplay(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDisplay(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除监测结果显示编号为"' + ids + '"的数据项？').then(function() {
        return delDisplay(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('alarm/display/export', {
        ...this.queryParams
      }, `display_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
